<?php
/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package lib
 * @subpackage http/Utils
 */
class BXDHttpUtils
{
    /**
	 * Metodo che restituisce l'ip reale passando eventuali proxy
	 *
	 * @return string
	 */
	public static function realIp()
	{
		if (!empty($_SERVER['HTTP_CLIENT_IP']))
		{
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else
		{
			$ip = $_SERVER['REMOTE_ADDR'];
		}

		return $ip;
	}

	/**
	 * Metodo che restituisce l'url reale a cui è stata fatta la chiamata
	 *
	 * @return string
	 */
	public static function realUrl()
	{
		if(!isset($_SERVER['REQUEST_URI']))
		{
			$serverrequri = $_SERVER['PHP_SELF'];
		}
		else
		{
			$serverrequri = $_SERVER['REQUEST_URI'];
		}

		$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
		$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
		$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":" . $_SERVER["SERVER_PORT"]);

		return $protocol . '://' . $_SERVER['SERVER_NAME'] . $port . $serverrequri;
	}

	/**
     * Metodo che verifica se un url esiste o meno attraverso il codice http di ritorno.
	 * Se la pagina risponderà con un codice 200 allora la richiesta è andata a buon fine
     *
     * @param string $url Url da verificare
	 *
     * @return boolean
     */
    public static function checkUrlExist($url)
	{
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);

		$resposneCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

		curl_close($ch);

		return (($resposneCode == 200) ? true : false);
    }

    /**
     * Metodo che ritorna il codice http della richiesta ad una pagina o contenuto web
     *
     * @param string $url Url della pagina da chiamare
	 *
     * @return int
     */
    public static function retrieveHeaderCode($url)
	{
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);
        
		return $httpCode;
    }
}
?>
